package com.plane.ticketse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plane.ticketse.utils.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.plane.ticketse.service.IRoleService;
import com.plane.ticketse.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * RoleController 前端控制器
 * @author 作者
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody Role role) {
        return Result.success(roleService.saveOrUpdate(role));
    }

    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(roleService.removeById(id));
    }

    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> list= roleService.list(queryWrapper);
        return Result.success(list);
    }

    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleService.getById(id));
    }

    /**
     * 获取所有角色map
     */
    @GetMapping("/roleMap")
    public Result roleMap(){
        Map<Integer,String> roleMap=new HashMap<>();
        // 获取全部数据
        List<Role> list = roleService.list();
        // 循环赋值
        for (Role role : list) {
            roleMap.put(role.getId(),role.getRoleName());
        }
        // 返回roleMap
        return Result.success(roleMap);
    }
}

